#ifndef PACKAGER_H
#define PACKAGER_H

#include <head/g_pch_core.h>
#include <support/pie.h>
#include <quazip5/JlCompress.h>
#include "m_fhs.h"

class Packager : public QObject
{
    Q_OBJECT
public:
    explicit Packager(QObject *parent = nullptr):
        QObject(parent){}

    void prepare(const QString &fileName)
    {
        Pie pie;
        if(pie.load(fileName))
        {
            QJsonArray items=pie.value("data").toArray();
            for(int i=0;i< items.size();i++)
            {
                QJsonObject item=items.at(i).toObject();
                QString target=item.value("dir").toString();
                QString output=item.value("zip_dir").toString();
                targetMap.insert(target,output);
            }

            if(targetMap.isEmpty())
                emit logMessage("target is Empty");
            else
            {
                emit logMessage(QString("Target Count %1, Now to process...")
                                .arg(targetMap.size()));
                processTarget();
            }
        }
        else
            emit logMessage("Config load Failure");
    }

    void processTarget()
    {
        if(targetMap.isEmpty())
            return;

        QStringList keys=targetMap.keys();
        foreach (QString key, keys) {
            QString targetDir=key;
            QString outputDir=targetMap.value(key);

            QString outputFile=outputDir + QDir::separator()
                    + QString("acc_%1.zip").arg(CurrentDate);

            emit logMessage("Current Target: " + targetDir);
            if(JlCompress::compressDir(outputFile,targetDir))
                emit logMessage(targetDir + "Compress Finished");
            else
                emit logMessage(targetDir + "Compress Failure");
        }
        emit processFinished();
    }

signals:
    void logMessage(const QString &message);
    void processFinished();

private:
    QMap<QString,QString> targetMap;
};

#endif // PACKAGER_H
